package org.osomit.sacct.session.token.impl;

import org.osomit.sacct.session.token.iface.TokenExpirationPolicy;
import org.osomit.sacct.session.token.iface.TokenState;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jul 1, 2009
 */
public class LifetimeTimeoutTokenExpirationPolicy implements TokenExpirationPolicy {
    private static final long serialVersionUID = -9010797464020063865L;

    // The time the token can live.
    private final long lifetimeInMilliSeconds;

    public LifetimeTimeoutTokenExpirationPolicy(final long lifetimeInMilliSeconds){
        this.lifetimeInMilliSeconds = lifetimeInMilliSeconds;
    }

    public long getLifetimeInMilliSeconds() {
        return this.lifetimeInMilliSeconds;
    }

    public boolean isExpired(TokenState tokenState) {
        return (tokenState == null)
            || (System.currentTimeMillis() - tokenState.getCreationTime() >= lifetimeInMilliSeconds); 
    }
}
